﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using WhatCatch.Core2.Model;

namespace WhatCatch.Core2
{
    public class CrawlerRobot
    {
        private RobotInfo _robotInfo;
        //private DataContainer _dataContainer;
        private ICatchQueue _catchQueue;
        private CrawlerLog _crawlerLog;
        private IHttpLoader _httpLoader;
        private IHtmlAnalyser _htmlAnalyser;
        private ListPageHandler _listPageHandler;
        private DetailPageHandler _detailPageHandler;

        public CrawlerRobot(RobotInfo robotInfo, ICatchQueue catchQueue)
        {
            _robotInfo = robotInfo;
            //_dataContainer = dataContainer;
            _catchQueue = catchQueue;
            _crawlerLog = new CrawlerLog();
            _httpLoader = new TestHttpLoader();
            //_httpLoader = new HttpLoader();
            _htmlAnalyser = new HtmlAnalyser();
            _listPageHandler = new ListPageHandler(this);
            _detailPageHandler = new DetailPageHandler(this);

            _crawlerLog.Info($"运行机器人【{_robotInfo.RobotKey}】");
        }

        /// <summary>
        /// 日志
        /// </summary>
        public CrawlerLog CrawlerLog { get { return _crawlerLog; } }
        /// <summary>
        /// Http加载器
        /// </summary>
        public IHttpLoader HttpLoader { get { return _httpLoader; } }
        /// <summary>
        /// Html解析器
        /// </summary>
        public IHtmlAnalyser HtmlAnalyser { get { return _htmlAnalyser; } }

        public int Handle()
        {
            //取一个列表url，标记读取
            var task = _catchQueue.TaskDequeue();
            if (task == null)
            {
                _crawlerLog.Info($"【{_robotInfo.RobotKey}】无明细页任务");
                Thread.Sleep(1000);
                return 0;
            }
            _crawlerLog.Info($"【{_robotInfo.RobotKey}】取到任务 {task.Url}");

            HandleTaskResponse response = null;
            if (task.HandleType == 1)
            {
                response = _listPageHandler.Handle(task);
            }
            else if (task.HandleType == 2)
            {
                response = _detailPageHandler.Handle(task);
            }

            //结果记录
            CatchResult result = new CatchResult();
            result.TaskId = task.TaskId;
            result.Url = task.Url;
            result.ResourceId = task.ResourceId;
            result.HandleType = task.HandleType;
            if (response != null)
            {
                if (response.AritlceListUrl != null)
                {
                    result.DetailListResult = response.AritlceListUrl;
                }
                if (response.PageListUrl != null)
                {
                    foreach (var li in response.PageListUrl)
                    {
                        result.PageListResult = response.PageListUrl;
                    }
                }
                if (response.DetailContext != null)
                {
                    result.DetailResult = response.DetailContext;
                }
            }
            _catchQueue.ResultEnqueue(result);

            _crawlerLog.Info($"【{_robotInfo.RobotKey}】任务处理完成 {task.Url}");
            return 1;
        }
    }
}
